package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import org.springframework.stereotype.Service;

/**
 * @author: menghao
 * @version: 1.0
 * @Date: 2025/09/17 9:27
 * @Description:
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {

    /**
     * 新增或更新 银行账户
     * @param dto
     */
    @Override
    public void upSert(BankAccountUpsertReqDTO dto) {
        // 实体转换
        BankAccount bankAccount = BeanUtil.toBean(dto, BankAccount.class);
        bankAccount.setId(UserContext.currentUserId());
        //银行账户始终是唯一的，所以先查询
        BankAccount dbBankAccount = this.lambdaQuery()
                .eq(BankAccount::getAccount, dto.getAccount())
                .one();
        if (ObjectUtil.isEmpty(dbBankAccount)) {
            // 新增
            this.save(bankAccount);
        }else {
            // 更新
            this.updateById(bankAccount);
        }
    }

    /**
     * 查询当前用户银行账户
     * @return
     */
    @Override
    public BankAccountResDTO queryCurrentUserBankAccount() {
        // 获取当前用户id
        Long userId = UserContext.currentUserId();
        BankAccount bankAccount = this.lambdaQuery()
                .eq(BankAccount::getId, userId)
                .one();
        return BeanUtil.toBean(bankAccount, BankAccountResDTO.class);
    }
}
